package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.ProductServiceWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.distinguish.ProductClassfiyContract;
import com.holyn.guofang.presenter.distinguish.ProductClassfiyPresenter;
import com.holyn.guofang.view.BaseFragment;
import com.luck.picture.lib.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/12.
 */

public class ProductServiceFragment extends BaseFragment implements BaseView, ProductClassfiyContract.ServiceView{
    private static final int REQUEST_ACTION_POST_PRODUCT_SERVICE_LIST = 0x0001;

    @BindView(R.id.rb_type)
    RadioButton rbType;
    @BindView(R.id.rb_group)
    RadioButton rbGroup;
    @BindView(R.id.rg_type_select)
    RadioGroup rgTypeSelect;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.et_search_value)
    EditText etSearchValue;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.btn_select_all)
    Button btnSelectAll;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private ProductClassfiyContract.Presenter presenter;

    private MyRecyclerAdapter recyclerAdapter;
    private List<ProductServiceWo.DataBean> dataList = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_product_service;
    }

    @Override
    protected void initData() {
        presenter = new ProductClassfiyPresenter(this);
        presenter.addServiceView(this);
    }

    @Override
    protected void initView() {
        rgTypeSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_type:
                        recyclerAdapter.notifyCodeChanged(true);
                        break;
                    case R.id.rb_group:
                        recyclerAdapter.notifyCodeChanged(false);
                        break;
                }
            }
        });

        recyclerAdapter = new MyRecyclerAdapter(dataList);
        recyclerAdapter.expandAll();//
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
        recyclerAdapter.bindToRecyclerView(rvList);
    }

    @OnClick({R.id.btn_submit, R.id.btn_search, R.id.btn_select_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                String result = recyclerAdapter.getCheckedResult();
                if (!TextUtils.isEmpty(result)){
                    Intent intent = getActivity().getIntent();
                    intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE, result);
                    if (rbType.isChecked()){
                        intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE_TYPE, ProductClassfiyActivity.TYPE_TYPE_CODE);
                    } else {
                        intent.putExtra(ProductClassfiyActivity.INTENT_KEY_SELECT_CODE_TYPE, ProductClassfiyActivity.TYPE_GROUP_CODE);
                    }
                    getActivity().setResult(Activity.RESULT_OK, intent);
                }
                getActivity().finish();
                break;
            case R.id.btn_search:
                String searchValue = etSearchValue.getText().toString().trim();
                presenter.postProductServiceList(REQUEST_ACTION_POST_PRODUCT_SERVICE_LIST, "", "", searchValue, -1, -1);
                break;
            case R.id.btn_select_all:
                recyclerAdapter.notifySelectAllChange();
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, ProductServiceWo productServiceWo) {
        if (productServiceWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            recyclerAdapter.notityAddNewDataChanged(productServiceWo.getData());
        }
    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<ProductServiceWo.DataBean, BaseViewHolder>{

        private boolean isShowType = true;
        private List<Boolean> isCheckList = new ArrayList<>();

        public MyRecyclerAdapter(@Nullable List<ProductServiceWo.DataBean> data) {
            super(R.layout.item_product_service, data);
            isCheckList.clear();
            for (int i = 0 ; i < data.size(); i++){
                isCheckList.add(false);
            }
        }

        @Override
        protected void convert(BaseViewHolder helper, ProductServiceWo.DataBean item) {
            if (isShowType){
                helper.setText(R.id.tv_code, "第"+item.getTypeCode()+"类");
            } else {
                helper.setText(R.id.tv_code, "第"+item.getGroupCode()+"类似群");
            }

            CheckBox cbSelect = helper.getView(R.id.cb_select);
            cbSelect.setChecked(isCheckList.get(helper.getAdapterPosition()));
            cbSelect.setText(item.getChineseName());
            cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    isCheckList.set(helper.getAdapterPosition(), isChecked);
                }
            });
        }

        public void notifyCodeChanged(boolean isShowType){
            this.isShowType = isShowType;
            notifyDataSetChanged();
        }

        public void notifySelectAllChange(){
            boolean isCurSelectAll = true;
            for (boolean isCheck : isCheckList){
                if (!isCheck){
                    isCurSelectAll = false;
                    break;
                }
            }
            for (int i = 0; i < getItemCount(); i++){
                isCheckList.set(i, !isCurSelectAll);
            }

            notifyDataSetChanged();

        }

        public void notityAddNewDataChanged(@Nullable List<ProductServiceWo.DataBean> data){
            getData().clear();
            getData().addAll(data);
            isCheckList.clear();
            for (int i = 0 ; i < data.size(); i++){
                isCheckList.add(false);
            }
            notifyDataSetChanged();
        }

        public String getCheckedResult(){
            String result = "";
            for (int i = 0; i < isCheckList.size(); i++){
                boolean isCheck = isCheckList.get(i);
                if (isCheck){
                    String selectValue = (isShowType ? getItem(i).getTypeCode() : getItem(i).getGroupCode());
                    if (TextUtils.isEmpty(result)){
                        result = result + selectValue;
                    } else {
                        if (!result.contains(selectValue)){
                            result = result + ";" + selectValue;
                        }
                    }

                }
            }
            return result;
        }
    }
 }
